package cn.tedu.straw.portal.controller;


import cn.tedu.straw.commons.model.User;
import cn.tedu.straw.portal.service.IUserService;
import cn.tedu.straw.portal.service.ex.ParameterValidationException;
import cn.tedu.straw.commons.vo.R;
import cn.tedu.straw.commons.vo.TagVO;
import cn.tedu.straw.commons.vo.TeacherVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-07-14
 */
@RestController
@RequestMapping("/v1/user")
@Slf4j
public class UserController {


    @Autowired
    private IUserService userService;

    @RequestMapping("/student/register")
    public R studentRegister(String inviteCode, @Validated User user, BindingResult bindingResult){


        if (bindingResult.hasErrors()) {
            String errorMessage = bindingResult
                    .getFieldError().getDefaultMessage();
            log.debug("validation has error : {}", errorMessage);
            throw new ParameterValidationException(errorMessage);
        }
            userService.registerStudent(user, inviteCode);
            return R.ok();

    }

    @GetMapping("")
    public R<List<TeacherVO>> getTags(){
        return R.ok(userService.getTeachers());
    }
}
